package com.example.esaclient;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class CrossPostActivity extends Activity {

	private TextView mTextView;
	private EditText mEditText;
	private ImageButton bttFB,bttTW;
	private Boolean postFB=false,postTW=false;
	private Integer esaUsrID;
    private GlobalSettings gs;
    private AlertDialogManager alert;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cross_post);
		
		bttFB = (ImageButton)findViewById(R.id.cpFB);
		bttFB.setBackgroundResource(R.drawable.fb_icon_off);
		bttTW = (ImageButton)findViewById(R.id.cpTW);
		bttTW.setBackgroundResource(R.drawable.tw_icon_off);
		
		mEditText=  (EditText)findViewById(R.id.txtPost);
		mTextView= (TextView)findViewById(R.id.tvCharsCount);
		mEditText.addTextChangedListener(mTextEditorWatcher);
		
		
		alert =  new AlertDialogManager();
		gs = new GlobalSettings(this);
		esaUsrID=new GlobalSettings(this).getPrefInt(GlobalSettings.PREF_ESA_USRID);
	}

	
	public void Crosspost(View v){
		
		if(!postFB && !postTW){
			
			alert.showAlertDialog(this,"",
					"You must select a social network before posting.", false);
			return;
		}
		else
		{
		if(postFB)
			   try {
					new CrossPostingFB(this).execute(esaUsrID,URLEncoder.encode(mEditText.getText().toString(), "utf-8"));
				} catch (UnsupportedEncodingException e) {
				
					e.printStackTrace();
				}
		if(postTW)
			   try {
					new CrossPostingTW(this).execute(esaUsrID,URLEncoder.encode(mEditText.getText().toString(), "utf-8"));
				} catch (UnsupportedEncodingException e) {
				
					e.printStackTrace();
				}
		finish();
		
		}
		
		
	}
	
	private final TextWatcher mTextEditorWatcher = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
           
           mTextView.setText(String.valueOf(s.length())+ " Characters.");
           if (s.length()>140){
        	   bttTW.setBackgroundResource(R.drawable.tw_icon_off);
   				postTW = false;
           }
        }

        public void afterTextChanged(Editable s) {
        }
};

	public void FBswitch(View v) {
		
		if(gs.getPrefBool(gs.PREF_KEY_FACEBOOK_LOGIN)==false)
		{

			alert.showAlertDialog(this,
					"Not connected on Facebook",
					"You must have an active Facebook account associated to ESA to post.", false);
			return;
		}
		
		if (postFB) {// set to off
			bttFB.setBackgroundResource(R.drawable.fb_icon_off);
			postFB = false;
		} else { // set to on
			bttFB.setBackgroundResource(R.drawable.fb_icon);
			postFB = true;
		}
	}

	public void TWswitch(View v) {
		if(gs.getPrefBool(gs.PREF_KEY_TWITTER_LOGIN)==false)
		{

			alert.showAlertDialog(this,
					"Not connected on Twitter",
					"You must have an active Twitter account associated to ESA to post.", false);
			return;
		}
		
		if (postTW) {// set to off
			bttTW.setBackgroundResource(R.drawable.tw_icon_off);
			postTW = false;
		} else { // set to on
			if (mEditText.getText().toString().length() < 140) {
				bttTW.setBackgroundResource(R.drawable.tw_icon);
				postTW = true;
			}
			else{
				 Toast.makeText(this, "The message length is over 140 characters.", Toast.LENGTH_SHORT).show();
			}
		}
	}

}